<?php
class profile extends module {
	public $title = "Edit and view profile module";
	public $permissions = array('*'=>array(ROLE_USER, ROLE_ADMIN, ROLE_SUPERADMIN));
	
	public function run() {
		$themesFD = array_combine(explode(",", AVAILABLE_THEMES), explode(",", AVAILABLE_THEMES));
		$languagesFD = array_combine(explode(",", AVAILABLE_LANGUAGES), explode(",", AVAILABLE_LANGUAGES));
		
		$form = new formConstructor($this, "profileBasic", "", _("Profile"));
		$form->addField("name", _("Full name"), new formString("/.{0,100}/"));
		$form->addField("company", _("Company"), new formString("/.{0,100}/"));
		$form->addField("_password", _("Password"), new formString("/.{0,100}/"), _("Do you want to change your password? Type new one here."));
//		$form->addField(null, _("Settings"), new formSeparator());
//		$form->addField("preferred_theme", _("Preferred theme"), new formSelectOne($themesFD));
//		$form->addField("preferred_language", _("Preferred language"), new formSelectOne($languagesFD));
		$form->addField(null, _("Save"), new formSubmit());

		if ($form->submit()) {
			if (count($form->errors) == 0) {
				$this->user->setProperty($form->getAllValues());
				if ($form->getValue("_password") != "") {
					$this->user->setProperty("password", md5($form->getValue("_password")));
				}
				new pageAlert($this, _("Profile saved."), PAGE_ALERT_SUCCESS);
				$form->setValue("_password", "");
			} else {
				new pageAlert($this, _("Wrong form filling"));
			}
		} else {
			$form->setValues($this->user->getProperty(array("name", "company")));
			$form->setValue('preferred_theme', $this->user->getPreferredTheme());
			$form->setValue('preferred_language', $this->user->getPreferredLanguage());
		}
		
		$form->write();
	}
}
?>